//
//  MainViewModel.m
//  CBT
//
//  Created by Mr.Zhang on 2018/11/21.
//  Copyright © 2018 Mr.Zhang. All rights reserved.
//

#import "MainViewModel.h"
#import "MainManager.h"

@interface MainViewModel()

@property (nonatomic, strong) MainModel         *mainModel;
@property (nonatomic, strong) MainIncomeModel   *incomeModel;
@property (nonatomic, strong) MainNewsModel     *mainNewsModel;

@end

@implementation MainViewModel

- (instancetype)init
{
    if (self = [super init]) {
        [self initialize];
    }
    return self;
}

- (void)initialize
{
    self.listSuccessObject     = [RACSubject subject];
    self.listFailureObject     = [RACSubject subject];
    self.incomeSuccessObject   = [RACSubject subject];
    self.bulletinSuccessObject = [RACSubject subject];
    
    
    self.incomeModel     = [MainIncomeModel new];
    self.mainModel       = [MainModel new];
    self.mainNewsModel   = [MainNewsModel new];
}

-(void)exchangeData {
   
    @weakify(self);
    //收益
    [MainManager mainIncomeWithUserID:[CBTLocalUserManager sharedInstance].userId block:^(id data, HttpRequestResult result, NSString *msg) {
        if (result == kHttpRequestResultNormal) {
            self_weak_.incomeModel = [MainIncomeModel mj_objectWithKeyValues:data[@"data"]];
            [self_weak_.incomeSuccessObject sendNext:self_weak_.incomeModel];
        }
    }];
    
    //已上架币种
    [MainManager mainCoinInfoListWithUserID:[CBTLocalUserManager sharedInstance].userId page:1 block:^(id data, HttpRequestResult result, NSString *msg) {
        self_weak_.mainModel = [MainModel mj_objectWithKeyValues:data[@"data"]];
        if (result == kHttpRequestResultNormal) {
            [self_weak_.listSuccessObject sendNext:self_weak_.mainModel];
        }else{
            [HFStatusView showFailureMessage:kLocalizedString(@"网络异常，请稍后重试", nil)];
            [self_weak_.listFailureObject sendNext:nil];
        }
    }];
}

-(void)getMainNewsList
{
    @weakify(self);
    
    //公告
    [MainManager mainGetNewsListWithType:@"1" page:1 block:^(MainNewsModel *mainNewsModel, HttpRequestResult result, NSString *msg) {
        if (result == kHttpRequestResultNormal && mainNewsModel.textArray.count > 0) {
            [self_weak_.bulletinSuccessObject sendNext:mainNewsModel];
        }
    }];
}

- (void)mainListWithPage:(int)page
{
    @weakify(self);
    
    [MainManager mainCoinInfoListWithUserID:[CBTLocalUserManager sharedInstance].userId page:page block:^(id data, HttpRequestResult result, NSString *msg) {
        if (result == kHttpRequestResultNormal) {
            self_weak_.mainModel = [MainModel mj_objectWithKeyValues:data[@"data"]];
            [self_weak_.listSuccessObject sendNext:self_weak_.mainModel];
        }else{
            [HFStatusView showFailureMessage:kLocalizedString(@"网络异常，请稍后重试", nil)];
            [self_weak_.listFailureObject sendNext:nil];
        }
    }];
}
@end
